const {resolve}  = require('path')
const HtmlWebpackPlugin = require('html-webpack-plugin')
const {CleanWebpackPlugin} = require('clean-webpack-plugin')

module.exports = {
    entry:'./src/index.ts',
    output:{
        filename:"js/index.js",
        path:resolve(__dirname,'build')
    },
    module:{
        rules:[
            {   //处理ts
                test:/\.ts$/,
                use:[
                    //配置babel
                    {
                        //加载器
                        loader:'babel-loader',
                        options:{
                            //预设环境
                            presets:[
                                [
                                    //指定预设环境
                                    "@babel/preset-env",
                                    {
                                        //兼容目标浏览器
                                        targets:{
                                            "chrome":58,
                                            "ie":11
                                        },
                                        //corejs的版本
                                        "corejs":3,
                                        //配置按需加载
                                        "useBuiltIns":"usage"
                                    }
                                ]
                            ]
                        }
                    },
                    //将ts转为js
                    {loader:'ts-loader'}
                ],
                exclude:/node_modules/
            },
            {
                //处理less
                test:/\.less$/,
                use:[
                    "style-loader",
                    "css-loader",
                    // 处理css的兼容性问题
                    {
                        // 加载器
                        loader:'postcss-loader',
                        options:{
                            postcssOptions:{
                                plugins:[
                                    [
                                        'postcss-preset-env',
                                        {
                                            browsers:'last 2 versions'
                                        }
                                    ]
                                ]
                            }
                        }
                    },
                    // 将less转为css
                    "less-loader"
                ],
                exclude:/node_modules/
            }
        ]
    },
    plugins:[
        new HtmlWebpackPlugin({
            template:'./src/index.html'
        }),
        new CleanWebpackPlugin()
    ],
    //用来告诉webpack 哪些文件可以导入
    resolve:{
        extensions:['.ts','.css','.js']
    },
    mode:'development'
}
